<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Stellt alle Funktionen zum Auflisten und Editieren der Spielarten zur Verfügung
 *
 * @author Robin
 */

include "redirector.php";
include "../ff_includes/class_database.php";
include "../ff_includes/class_gametype.php";

$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ) {
  case 'list':
    listGametypes();
    break;
  case 'new':
    newGametype();
    break;
  case 'edit':
    editGametype();
    break;
  case 'save':
    saveGametype();
    break;
  case 'delete':
    deleteGametype();
    break;
  default:
    listGametype();
}

function listGametypes() {
    $db = new Database();
    $gametypeList = Gametype::getGametypeList();
    $numbGametypes = $gametypeList['totalRows'];
    $gametypes = $gametypeList['gametypeList'];
    
    require("templates/gametype_list.php");
}

function newGametype() {
    $db = new Database();
    $gametype = new Gametype();
    
    require("templates/gametype_edit.php");
}

function editGametype() {
    $db = new Database();
    $gametypeId = (int)$_GET['gametypeId'];
    $gametype = Gametype::getGametypeById($gametypeId);
    
    require("templates/gametype_edit.php");
}

function saveGametype() {
    // speichern
    if ( isset( $_POST['saveChanges'] ) ) {
        $gametype = new Gametype($_POST);
        // update
        if ($gametype->getId() != 0) {
            $gametype->update();
        }
        // neuer Eintrag
        else {
            $gametype->insert();
        }
        header( "Location: gametype.php?action=list" );
    }
    // abbrechen
    elseif ( isset( $_POST['cancel'] ) ) {
        header( "Location: gametype.php?action=list" );
    }
}

function deleteGametype() {
    $gametype = Gametype::getGametypeById((int)$_GET['gametypeId']);
    $gametype->delete();
    header( "Location: gametype.php?action=list" );
}
?>
